# -*- coding: utf-8 -*-
from google.appengine.ext import db

from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from appengine_django.models import BaseModel as _BaseModel
#from appengine_django.models import BaseModel

import logging

from util import get_today

class BaseModel(_BaseModel):
    id = db.IntegerProperty()

    def put(self):
        _BaseModel.put(self)
        self.id = self.key().id()
        _BaseModel.put(self)

    @classmethod
    def get_using_id(self, id):
        str_query = 'SELECT * FROM ' + self.kind() + ' WHERE id = :1'
        query = db.GqlQuery(str_query, id)
        return query.fetch(10)[0]

    @property
    def get_absolute_url(self):
        detail_name = self.__class__.__name__ + '_' + 'detail'
        return HttpResponseRedirect(reverse(detail_name.lower(), args=(self.id,)))

#####

class TransactionManager(object):
    @staticmethod
    def is_exist(first_name):
        return True

CATEGORY_TYPE = ['option1', 'option2', 'option13', 'option4']
RECORD_TYPE = ['Expense', 'Deposit']
LOCATION_TYPE = ['A', 'K']
ACCOUNT_TYPE = ['ORG', 'PTY']
TODAY = get_today()

class Transaction(BaseModel):
    date = db.DateProperty(
                           verbose_name='날짜',
                           default=TODAY
                           )
    category = db.StringProperty(
                                 verbose_name='항목',
                                 choices=CATEGORY_TYPE,
                                 default=CATEGORY_TYPE[0]
                                 )
    detail = db.TextProperty(verbose_name='내용')
    type = db.StringProperty(
                             verbose_name='출금/입금',
                             choices=RECORD_TYPE,
                             default=RECORD_TYPE[0]
                             )
    amount = db.IntegerProperty(
                                 verbose_name='금액',
                                 default=0
                                 )
    balance = db.IntegerProperty(
                                 verbose_name='잔액',
                                 default=0
                                 )
    note = db.TextProperty(verbose_name='비고')
    account = db.StringProperty(
                                verbose_name='통장구분',
                                choices=ACCOUNT_TYPE,
                                default=ACCOUNT_TYPE[0]
                                )
    location = db.StringProperty(
                                 verbose_name='구분',
                                 choices=LOCATION_TYPE,
                                 default=LOCATION_TYPE[0]
                                 )
    last_updated = db.DateTimeProperty(auto_now=True)

class MonthTransactionManager(object):
    @staticmethod
    def get_month(year, month):
        m = MonthTransaction.gql('WHERE year=:year AND month=:month', year=year, month=month).fetch(1)
        if m: return m[0]
        return None

class MonthTransaction(BaseModel):
    year = db.IntegerProperty(default=0)
    month = db.IntegerProperty(default=0)
    expense = db.IntegerProperty(default=0)
    deposit = db.IntegerProperty(default=0)
    balance = db.IntegerProperty(default=0)
    k_expense = db.IntegerProperty(default=0)
    a_expense = db.IntegerProperty(default=0)

